package com.py.credit.clean.code;

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 自然人库中表规则增加证件类型校验,如果是身份证,
 * 则校验身份证格式,如果是其他的,则必填
 */
public class TableZjlxCheck {

    public final static String ZJLX_KEY = "ZJLX";
    public final static String CF_FDDBRZJLX =  "CF_FDDBRZJLX";
    public final static String XK_FDDBRZJLX =  "XK_FDDBRZJLX";
    public final static String CF_ZJLX_KEY = "CF_XDR_ZJLX";
    public final static String XK_ZJLX_KEY = "XK_XDR_ZJLX";

    public final static String ZJLX_SFZ = "身份证";

    public final static String B000002_KEY = "B000003";
    public final static String XK_FDDBRZJHM_KEY = "XK_FDDBRZJHM";
    public final static String CF_FDDBRZJHM_KEY = "CF_FDDBRZJHM";
    
    public final static String IDCARD_RULE_PACKAGE = "com.py.credit.clean.code.IdcardValidator";

    public static boolean run(Map<String, Object> map, String extend) {
        String key;
        String cfKey = (String) map.get(CF_ZJLX_KEY);
        String xkKey = (String) map.get(XK_ZJLX_KEY);
        String frcfKey = (String) map.get(CF_FDDBRZJLX);
        String frxkKey = (String) map.get(XK_FDDBRZJLX);
        //证件类型非空判断(二者有一个不为空)
        if (StringUtils.isNotBlank(cfKey)) {
            key = cfKey;
        } else if (StringUtils.isNotBlank(xkKey)) {
            key = xkKey;
        }else if (StringUtils.isNotBlank(frcfKey)) {
            key = frcfKey;
        }else if (StringUtils.isNotBlank(frxkKey)) {
            key = frxkKey;
        }  else {
            key = "";
        }
        if(String.valueOf(map.get("tableId")).equals("210002")||String.valueOf(map.get("tableId")).equals("210003")) {
        	  if (ZJLX_SFZ.equals(key)) {
                  try {
                      Class<?> c = Class.forName(IDCARD_RULE_PACKAGE);
                      Method m = c.getMethod("run", String.class, String.class);
                      String data = (String) map.get(B000002_KEY);
                      return (boolean) m.invoke(c.newInstance(), data, "");
                  } catch (Exception e) {
                      return false;
                  }
              } else if (!StringUtils.isNotEmpty(key)) {
                  String data = (String) map.get(B000002_KEY);
                  if (null == data || "".equals(data.trim())) {
                      return true;
                  } else {
                      return false;
                  }
              }else {
            	  String data = (String) map.get(B000002_KEY);
            	  if (null != data || !"".equals(data.trim())) {
                      return true;
                  }else {
                	  return false;
                  }
              }
        }else if(String.valueOf(map.get("tableId")).equals("210000")) {
        	  if (ZJLX_SFZ.equals(key)) {
                  try {
                      Class<?> c = Class.forName(IDCARD_RULE_PACKAGE);
                      Method m = c.getMethod("run", String.class, String.class);
                      String data = (String) map.get(XK_FDDBRZJHM_KEY);
                      return (boolean) m.invoke(c.newInstance(), data, "");
                  } catch (Exception e) {
                      return false;
                  }
              } else if (!StringUtils.isNotEmpty(key)) {
                  String data = (String) map.get(XK_FDDBRZJHM_KEY);
                  if (null == data || "".equals(data.trim())) {
                      return true;
                  } else {
                      return false;
                  }
              }else {
            	  String data = (String) map.get(XK_FDDBRZJHM_KEY);
            	  if (null != data || !"".equals(data.trim())) {
                      return true;
                  }else {
                	  return false;
                  }
              }
        }else if(String.valueOf(map.get("tableId")).equals("210001")) {
        	  if (ZJLX_SFZ.equals(key)) {
                  try {
                      Class<?> c = Class.forName(IDCARD_RULE_PACKAGE);
                      Method m = c.getMethod("run", String.class, String.class);
                      String data = (String) map.get(CF_FDDBRZJHM_KEY);
                      return (boolean) m.invoke(c.newInstance(), data, "");
                  } catch (Exception e) {
                      return false;
                  }
              } else if (!StringUtils.isNotEmpty(key)) {
                  String data = (String) map.get(CF_FDDBRZJHM_KEY);
                  if (null == data || "".equals(data.trim())) {
                      return true;
                  } else {
                      return false;
                  }
              }else {
            	  String data = (String) map.get(CF_FDDBRZJHM_KEY);
            	  if (null != data || !"".equals(data.trim())) {
                      return true;
                  }else {
                	  return false;
                  }
              }
        }
      
        return false;

    }

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("XK_FDDBRZJLX", "");
        map.put("XK_FDDBRZJHM", "");
        map.put("tableId","210000");
        boolean b = run(map, "");
        System.out.println(b);
    }
}
